/**
 * Created by IntelliJ IDEA.
 * User: bl4ckt1ger
 * Date: 19/09/11
 * Time: 23.51
 * To change this template use File | Settings | File Templates.
 */
var Rk = 6372795.47760; // mean radius of the earth (m)

function findCrowsFliesDistance(coords) {
    var lat1, lon1, lat2, lon2, dlat, dlon, a, c, dk;

    // convert coordinates to radians
    lat1 = deg2rad(coords.lat_partenza);
    lon1 = deg2rad(coords.long_partenza);
    lat2 = deg2rad(coords.lat_arrivo);
    lon2 = deg2rad(coords.long_arrivo);

    // find the differences between the coordinates
    dlat = lat2 - lat1;
    dlon = lon2 - lon1;

    // here's the heavy lifting
    a  = Math.pow(Math.sin(dlat/2),2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon/2),2);
    c  = 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1-a)); // great circle distance in radians
    dk = c * Rk; // great circle distance in m

    return Math.round(dk);
}

// convert degrees to radians
function deg2rad(deg) {
    var rad = deg * Math.PI/180; // radians = degrees * pi/180
    return rad;
}
